package com.Jian.reggie.controller;

import com.Jian.reggie.common.R;
import com.Jian.reggie.dto.DishDto;
import com.Jian.reggie.dto.SetmealDto;
import com.Jian.reggie.impl.*;
import com.Jian.reggie.service.CategoryService;
import com.Jian.reggie.service.SetmealDishService;
import com.Jian.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: SetmealController
 * Package: com.Jian.reggie.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/27 15:08
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
  @Autowired
  private SetmealService setmealService;
  @Autowired
  private CategoryService categoryService;
  @Autowired
  private SetmealDishService setmealDishService;
  
  /**
   * 新增套餐，同时保存对应菜品数据
   */
  @PostMapping
  public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
    // 新增套餐，同时保存对应菜品数据
    setmealService.saveWithSetMeal(setmealDto);
    return R.success("新增套餐成功");
  }
  
  /**
   * 分页查询
   */
  @GetMapping("/page")
  public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
    // 将setmeal表对应的数据查出来
    Page<Setmeal> setmealPage = new Page<>(page, pageSize);
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(name != null, Setmeal::getName, name);
    setmealService.page(setmealPage, queryWrapper);
    
    // 将setmeal表对应的数据转换成setmealDto
    Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
    BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
    // 查找出对应的分类名称,放到setmealDto中
    List<SetmealDto> setmealDtos = setmealPage.getRecords().stream().map((setmeal -> {
      SetmealDto setmealDto = new SetmealDto();
      BeanUtils.copyProperties(setmeal, setmealDto);
      Category categoryServiceById = categoryService.getById(setmeal.getCategoryId());
      setmealDto.setCategoryName(categoryServiceById.getName());
      return setmealDto;
    })).collect(Collectors.toList());
    
    setmealDtoPage.setRecords(setmealDtos);
    return R.success(setmealDtoPage);
  }
  
  /**
   * 根据id删除套餐
   */
  @DeleteMapping
  public R<String> deleteSetmeal(@RequestParam List<Long> ids) {
    LambdaQueryWrapper<Setmeal> setmealqueryWrapper = new LambdaQueryWrapper<>();
    LambdaQueryWrapper<Setmeal> tempSetmealqueryWrapper = new LambdaQueryWrapper<>();
    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
    
    // 获取到ids 在 setmeal中对应的数据
    setmealqueryWrapper.in(Setmeal::getId, ids);
    tempSetmealqueryWrapper.in(Setmeal::getId, ids);
    queryWrapper.in(SetmealDish::getSetmealId, ids);
    // 如果ids中的套餐正在售卖，不能删除
    if (setmealService.count(tempSetmealqueryWrapper.eq(Setmeal::getStatus, 1)) > 0) {
      return R.error("该套餐正在售卖，不能删除");
    }
    
    setmealService.remove(setmealqueryWrapper);
    setmealDishService.remove(queryWrapper);
    
    return R.success("删除套餐成功");
  }
  
  /**
   * 根据id修改套餐
   */
  @GetMapping("/{id}")
  public R<SetmealDto> getSetmeal(@PathVariable Long id) {
    SetmealDto setmealDto = new SetmealDto();
    Setmeal setmeal = setmealService.getById(id);
    BeanUtils.copyProperties(setmeal, setmealDto);
    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(SetmealDish::getSetmealId, id);
    List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
    setmealDto.setSetmealDishes(setmealDishes);
    return R.success(setmealDto);
  }
  
  /**
   * 保存修改的套餐
   */
  @PutMapping
  public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto) {
    setmealService.updateWithSetMeal(setmealDto);
    return R.success("修改套餐成功");
  }
  
  /**
   * 根据id修改套餐状态
   */
  @PostMapping("/status/{status}")
  public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.in(Setmeal::getId, ids);
    
    Setmeal setmeal = new Setmeal();
    setmeal.setStatus(status);
    
    setmealService.update(setmeal, queryWrapper);
    return R.success("修改套餐状态成功");
  }
  
  /**
   * 查询套餐对应的菜品
   */
  @GetMapping("/list")
  public R<List<Setmeal>> getSetmealDish(Long categoryId, int status) {
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(categoryId != null, Setmeal::getCategoryId, categoryId);
    queryWrapper.eq(categoryId != null, Setmeal::getStatus, status);
    List<Setmeal> list = setmealService.list(queryWrapper);
    
    return R.success(list);
  }
  
  /**
   * 查询套餐对应的菜品
   */
  @GetMapping("/dish/{id}")
  public R<List<Setmeal>> getSetmealById(@PathVariable Long id) {
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Setmeal::getId, id);
    List<Setmeal> list = setmealService.list(queryWrapper);
    return R.success(list);
  }
}
